package com.gxgy.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gxgy.base.BaseAllInterface;
import com.gxgy.entity.Book;
import com.gxgy.uilt.IfVersionException;

/**
 * @description: 图书接口
 * @author: Qin Fengang
 * @date: 2021-11-18
 */
@Controller
public class BookController extends BaseAllInterface{
	
	/**
	 * @description: 图书列表
	 * @author: Qin Fengang
	 * @date: 2021-11-18
	 */
	@RequestMapping("/bookList")
	public String flndAllBook(Model model) {
		List<Book> book = bookSerivce.queryAllBook();
		model.addAttribute("book",book);
		return "page/book/book";
	}
	
	
	/**
	 * @description: 图书添加
	 * @author: Qin Fengang
	 * @date: 2021-11-18
	 */
	@RequestMapping("/bookAdd")
	public String addBook() {
		return "page/book/bookAdd";
	}
	
	@RequestMapping("/doBooAdd")
	public String doAdd( String bookName,String bookType, String bookAuthor
			, String bookDescribe, String enerateTime,Model model) {
		if (bookName.isEmpty()) {
			model.addAttribute("msg","图书名称不能为空");
			return "page/book/bookAdd";
		}
		if (bookType.isEmpty()) {
			model.addAttribute("msg","图书类型不能为空");
			return "page/book/bookAdd";
		}
		if (bookAuthor.isEmpty()) {
			model.addAttribute("msg","图书作者不能为空");
			return "page/book/bookAdd";
		}
		if (bookDescribe.isEmpty()) {
			model.addAttribute("msg","图书描述不能为空");
			return "page/book/bookAdd";
		}
		if (enerateTime.isEmpty()) {
			model.addAttribute("msg","录入日期不能为空");
			return "page/book/bookAdd";
		}
		bookSerivce.addBook(bookName, bookType, bookAuthor, bookDescribe, enerateTime, model);
		return "redirect:bookList";
	}
	
	@RequestMapping("/edit")
	public String editBook(String id,Model model) {
		Book book = bookSerivce.queryBookById(id, model);
		model.addAttribute("book",book);
		return "page/book/bookEdit";
		
	}
	
	@RequestMapping("/doEditBook")
	public String doEditBook(String id, String bookName,String bookType, String bookAuthor
			, String bookDescribe, String enerateTime,Model model) {
		if (id.isEmpty()) {
			model.addAttribute("msg","图书编号不能为空");
			return "page/book/bookAdd";
		}
		if (bookName.isEmpty()) {
			model.addAttribute("msg","图书名称不能为空");
			return "page/book/bookAdd";
		}
		if (bookType.isEmpty()) {
			model.addAttribute("msg","图书类型不能为空");
			return "page/book/bookAdd";
		}
		if (bookAuthor.isEmpty()) {
			model.addAttribute("msg","图书作者不能为空");
			return "page/book/bookAdd";
		}
		if (bookDescribe.isEmpty()) {
			model.addAttribute("msg","图书描述不能为空");
			return "page/book/bookAdd";
		}
		if (enerateTime.isEmpty()) {
			model.addAttribute("msg","录入日期不能为空");
			return "page/book/bookAdd";
		}
		bookSerivce.updateBook(id, bookName, bookType, bookAuthor, bookDescribe, enerateTime, model);
		return "redirect:bookList";
	}

}
